class Solution {
public:
    int StrToInt(string str) {
        string ans;
        int begin = 0;
        int end = str.size();
        long long result = 0;
        int sign = 1;
        while (begin < end)
        {
            if (str[begin] >= '0' && str[begin] <= '9')
            {
                result = result * 10 + (str[begin] - '0');

            }
            else if (str[begin] == '+')
            {

            }
            else if (str[begin] == '-')
            {
                sign = -1;

            }
            else if (str[begin] == ' ')
            {
                continue;
            }
            else return 0;
            begin++;
        }
        return result * sign;

    }
};



class Solution {
public:
    string addStrings(string num1, string num2) {
        if (num1 == "0")
        {
            return num2;
        }
        if (num2 == "0")
        {
            return num1;
        }
        if (num1 == "0" && num2 == "0")
        {
            return "0";
        }
        int end1 = num1.size() - 1, end2 = num2.size() - 1;
        int carray = 0;
        string sum;
        while (end1 >= 0 || end2 >= 0)
        {
            int val1 = end1 >= 0 ? num1[end1] - '0' : 0;
            int val2 = end2 >= 0 ? num2[end2] - '0' : 0;
            int tmp = val1 + val2 + carray;
            carray = tmp / 10;
            tmp %= 10;
            char s = tmp + '0';
            sum.push_back(s);
            end1--;
            end2--;
        }
        if (carray == 1)
        {
            char s = carray + '0';
            sum.push_back(s);
        }
        reverse(sum.begin(), sum.end());
        return sum;
    }
};


class Solution {
public:
    void reverseString(string& s, int sbegin, int send) {
        while (sbegin < send)
        {
            swap(s[sbegin], s[send]);
            sbegin++;
            send--;
        }
    }
    string reverseWords(string s) {
        int sbegin = 0, send = 0;
        int begin = 0, end = s.size() - 1;
        while (begin <= end)
        {

            if (s[begin] == ' ')
            {
                send = begin - 1;
                reverseString(s, sbegin, send);
                sbegin = begin + 1;
            }
            begin++;
        }
        send = begin - 1;
        reverseString(s, sbegin, send);
        return s;
    }
};


char* multiply(char* num1, char* num2) {
    int len_num1 = strlen(num1), len_num2 = strlen(num2);
    int* arr = (int*)malloc(sizeof(int) * (len_num1 + len_num2 + 3));
    char* str = (char*)malloc(sizeof(char) * (len_num1 + len_num2 + 3));
    memset(str, 0, sizeof(char) * (len_num1 + len_num2 + 3));
    memset(arr, 0, sizeof(int) * (len_num1 + len_num2 + 3));
    if ((len_num1 == 1 && num1[0] == '0') || (len_num2 == 1 && num2[0] == '0'))
    {
        str[0] = '0', str[1] = 0;
        return str;
    }
    for (int i = len_num1 - 1; i >= 0; i--)
    {
        int val1 = num1[i] - '0';
        for (int j = len_num2 - 1; j >= 0; j--)
        {
            int val2 = num2[j] - '0';
            arr[i + j + 1] += val1 * val2;
        }
    }
    for (int i = len_num1 + len_num2 - 1; i > 0; i--)
    {
        arr[i - 1] += arr[i] / 10;
        arr[i] %= 10;
    }
    int index = arr[0] == 0 ? 1 : 0;
    int length = 0;
    while (index < len_num1 + len_num2)
    {
        str[length++] = arr[index++];
    }
    for (int i = 0; i < length; i++)
    {
        str[i] += '0';
    }

    return str;
}